import os

TARGET_PATH="user"
RS_PATH="kernel/src/link_app.rs"

def insert_app_data():
    # Read bins from target path
    bins = []
    for file in os.listdir(TARGET_PATH):
        if file.startswith("_"):
            bins.append(file)

    # Write bins to link_app.rs
    with open(RS_PATH, "w") as f:
        f.write(
        f"""static NUM_APP: usize = {len(bins)};

pub fn get_user_programs() -> [&'static [u8]; NUM_APP] {{
    let mut files: [&'static [u8]; NUM_APP] = [&[]; NUM_APP];
""")
        for i, bin in enumerate(bins):
            f.write(f"    files[{i}] = include_bytes!(\"../../{TARGET_PATH}/{bin}\");\n")
        f.write(f"    files\n}}\n")

if __name__ == '__main__':
    insert_app_data()